package cn.bandeqing.store.rpc.impl;

import cn.bandeqing.base.domain.CurrentAdminUser;
import cn.bandeqing.base.resp.Resp;
import cn.bandeqing.base.resp.Table;
import cn.bandeqing.finance.resp.LabelValue;
import cn.bandeqing.finance.resp.LabelValueStr;
import cn.bandeqing.frame.exception.CodeException;
import cn.bandeqing.store.entity.Store;
import cn.bandeqing.store.req.StoreSaveOrUpdateReq;
import cn.bandeqing.store.req.StoreSelectReq;
import cn.bandeqing.store.req.StoreTableReq;
import cn.bandeqing.store.req.StoreUserSelectReq;
import cn.bandeqing.store.resp.StoreTable;
import cn.bandeqing.store.rpc.IStoreRpc;
import cn.bandeqing.store.service.IStoreService;
import cn.bandeqing.stp.StpAdminUtil;
import cn.bandeqing.user.entity.UsStatus;
import cn.bandeqing.user.service.IUserService;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import io.github.linpeilie.Converter;
import jakarta.annotation.Resource;
import org.apache.dubbo.config.annotation.DubboService;
import org.dromara.hutool.core.lang.Assert;
import org.dromara.hutool.core.text.StrUtil;

import java.util.List;

import static cn.bandeqing.store.entity.table.StoreTableDef.STORE;
import static cn.bandeqing.user.entity.table.UserTableDef.USER;

@DubboService
public class StoreRpcImpl implements IStoreRpc {

    @Resource
    private IStoreService storeService;
    @Resource
    private IUserService userService;
    @Resource
    private Converter converter;

    @Override
    public Resp<Table<StoreTable>> table(StoreTableReq req) {
        QueryWrapper qw = QueryWrapper.create()
                .where(STORE.STORE_ID.eq(req.getStoreId()))
                .where(STORE.STORE_NAME.eq(req.getStoreName()))
                .where(STORE.STORE_ADDRESS.eq(req.getStoreAddress()))
                .orderBy(STORE.STORE_ID.desc());

        Page<StoreTable> page = storeService.pageAs(Page.of(req.getCurrent(), req.getPageSize()), qw, StoreTable.class);
        return Resp.success(Table.of(page.getRecords(), page.getTotalRow()));
    }

    @Override
    public void saveOrUpdate(StoreSaveOrUpdateReq req) {
        Store store = converter.convert(req, Store.class);
        boolean b = storeService.saveOrUpdate(store);
        Assert.isTrue(b, () -> new CodeException("storeService.saveOrUpdate(store)"));
    }

    @Override
    public Resp<List<LabelValue>> userSelect(StoreUserSelectReq req) {
        List<LabelValue> labelValues = userService.listAs(QueryWrapper.create()
                .select(USER.US_ID.as(LabelValue::getValue), USER.US_NICK_NAME.as(LabelValue::getLabel))
                .where(USER.US_STATUS.eq(UsStatus.ok)), LabelValue.class);
        return Resp.success(labelValues);
    }

    @Override
    public Resp<List<LabelValue>> select(StoreSelectReq req) {
        CurrentAdminUser current = StpAdminUtil.getCurrent();
        if (current.checkStoreAccount()) {
            List<LabelValue> labelValues = storeService.listAs(QueryWrapper.create()
                    .select(STORE.STORE_NAME.as(LabelValue::getLabel),
                            STORE.STORE_ID.as(LabelValue::getValue))
                    .where(STORE.CITY_CODE.eq(req.getCityCode()))
                    .where(STORE.CITY_PARTNER_US_ID.eq(current.getUsId())
                            .or(STORE.SHOPKEEPER_US_ID.eq(current.getUsId()))
                            .or(STORE.SHOPKEEPER_US_ID.eq(current.getUsId()))), LabelValue.class);
            return Resp.success(labelValues);
        }

        List<LabelValue> labelValues = storeService.listAs(QueryWrapper.create()
                .select(STORE.STORE_NAME.as(LabelValue::getLabel),
                        STORE.STORE_ID.as(LabelValue::getValue))
                .where(STORE.CITY_CODE.eq(req.getCityCode())), LabelValue.class);
        return Resp.success(labelValues);
    }

    @Override
    public Resp<List<LabelValueStr>> selectDoyin(StoreSelectReq req) {
        CurrentAdminUser current = StpAdminUtil.getCurrent();
        if (current.checkStoreAccount()) {
            List<LabelValueStr> labelValues = storeService.listAs(QueryWrapper.create()
                    .select(STORE.STORE_NAME.as(LabelValueStr::getLabel),
                            STORE.STORE_ID.as(LabelValueStr::getValue))
                    .where(STORE.CITY_CODE.eq(req.getCityCode()))
                    .where(STORE.CITY_PARTNER_US_ID.eq(current.getUsId())
                            .or(STORE.SHOPKEEPER_US_ID.eq(current.getUsId()))
                            .or(STORE.SHOPKEEPER_US_ID.eq(current.getUsId())))
                    .where(STORE.DOUYIN_POI_ID.isNotNull().and(STORE.DOUYIN_POI_ID.ne(StrUtil.EMPTY))), LabelValueStr.class);
            return Resp.success(labelValues);
        }

        List<LabelValueStr> labelValues = storeService.listAs(QueryWrapper.create()
                .select(STORE.STORE_NAME.as(LabelValueStr::getLabel),
                        STORE.DOUYIN_POI_ID.as(LabelValueStr::getValue))
                .where(STORE.CITY_CODE.eq(req.getCityCode()))
                .where(STORE.DOUYIN_POI_ID.isNotNull().and(STORE.DOUYIN_POI_ID.ne(StrUtil.EMPTY))), LabelValueStr.class);
        return Resp.success(labelValues);
    }

}
